package com.lkbaby.flowable;

import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class MyProcess {

	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;

	/**
	 * 流程部署
	 *
	 * @throws Exception
	 */
	@RequestMapping("/deploy")
	public void testDeploy() {

		// 主流程部署
		List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey("call-activity-master").list();
		for (ProcessDefinition definition : processDefinitions) {
			System.out.println(definition.getDeploymentId());
			repositoryService.deleteDeployment(definition.getDeploymentId(),true);
		}

		Deployment deploy = repositoryService.createDeployment()
				.addClasspathResource("processes/调用活动_主流程.bpmn20.xml")
				.name("调用主流程")
				.deploy();
		System.out.println("deploy.getId() = " + deploy.getId());
		System.out.println("deploy.getName() = " + deploy.getName());
		System.out.println("部署开始的时间：" + new Date());
		//TimeUnit.MINUTES.sleep(3);

		// 子流程部署
		processDefinitions = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey("call-activity-slave").list();
		for (ProcessDefinition definition : processDefinitions) {
			System.out.println(definition.getDeploymentId());
			repositoryService.deleteDeployment(definition.getDeploymentId(),true);
		}

		deploy = repositoryService.createDeployment()
				.addClasspathResource("processes/调用活动_子流程.bpmn20.xml")
				.name("调用子流程")
				.deploy();
		System.out.println("deploy.getId() = " + deploy.getId());
		System.out.println("deploy.getName() = " + deploy.getName());
		System.out.println("部署开始的时间：" + new Date());
		//TimeUnit.MINUTES.sleep(3);

	}

	/**
	 * 启动流程
	 *
	 * @throws Exception
	 */
	@RequestMapping("/startFlow")
	public void startFlow() {
		ProcessDefinition definition = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey("call-activity-master").singleResult();
		runtimeService.startProcessInstanceById(definition.getId());
	}

	/**
	 * 完成用户任务
	 *
	 * @param userKey
	 */
	@RequestMapping("/completeOne")
	public void completeOne(@RequestParam String userKey) {
		Task task = taskService.createTaskQuery()
				.processDefinitionKey("call-activity-master")
				.taskAssignee(userKey)
				.singleResult();
		Map<String ,Object> map = new HashMap<>();
		map.put("status",false);
		taskService.complete(task.getId(),map);
	}

}
